package com.bwie.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.mapper.TbProdMapper;
import com.bwie.pojo.TbProd;
import com.bwie.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @projectName: server-2211a-shop
 * @package: com.bwie.task
 * @className: ProdDownTask
 * @author: 杨珂
 * @description: TODO
 * @date: 2025/5/29 11:09
 * @version: 1.0
 */
@Slf4j
@Component
public class ProdDownTask {

    @Autowired
    TbProdMapper tbProdMapper;

    /**
     * @param :
     * @return void
     * @author :杨珂
     * @description 每分钟检查一次商品
     * @date 2025/5/29 11:10
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void prodDown() {

        log.info("开始执行商品下架");

        List<TbProd> tbProds = tbProdMapper.selectList(
                new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdStatus, 1)
                        .eq(TbProd::getDownType, 1)
                        .le(TbProd::getDownTime, new Date())
        );


        for (TbProd tbProd : tbProds) {

            tbProd.setProdStatus(0);
            tbProdMapper.updateById(tbProd);
        }

        log.info("下架完成");
    }

}
